﻿using CLCommuConfig;
using CLVisionModbus;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace CLVisionCommu.CLCommuTcpIP
{
    public class CdatToCommuJson
    {
        public JObject cdatToJson(List<IObject> objects)
        {
            JObject rootClass = new JObject();
            List<JObject> Object = new List<JObject>();

            for (int i = 0; i < objects.Count(); i++)
            {
                var objItems = objects[i].ObjectItemList;
                JProperty kewValue = null;
                JObject Objects = new JObject();
                foreach (var objItem in objItems)
                {
                    if (objItem.Array == 1)
                    {
                        if (objItem.Type == "USHORT")
                        {
                            kewValue = new JProperty(objItem.Name, 0);
                        }
                        else if (objItem.Type == "INT")
                        {
                            kewValue = new JProperty(objItem.Name, 0);
                        }
                        else if (objItem.Type == "DOUBLE")
                        {
                            kewValue = new JProperty(objItem.Name, 0);
                        }
                        else if (objItem.Type == "STRING")
                        {
                            kewValue = new JProperty(objItem.Name, "");
                        }
                    }
                    else if(objItem.Array > 1)
                    {
                        if (objItem.Type == "USHORT")
                        {
                            ushort[] ushorts = new ushort[objItem.Array];
                            for (int j = 0; j < ushorts.Length; j++)
                            {
                                ushorts[j] = 0;
                            }
                            kewValue = new JProperty(objItem.Name, ushorts);
                        }
                        else if (objItem.Type == "INT")
                        {
                            int[] ints = new int[objItem.Array];
                            for (int j = 0; j < ints.Length; j++)
                            {
                                ints[j] = 0;
                            }
                            kewValue = new JProperty(objItem.Name, ints);
                        }
                        else if (objItem.Type == "DOUBLE")
                        {
                            double[] doubles = new double[objItem.Array];
                            for (int j = 0; j < doubles.Length; j++)
                            {
                                doubles[j] = 0;
                            }
                            kewValue = new JProperty(objItem.Name, doubles);
                        }
                        else if (objItem.Type == "STRING")
                        {
                            string[] strings = new string[objItem.Array];
                            for (int j = 0; j < strings.Length; j++)
                            {
                                strings[j] = string.Empty;
                            }
                            kewValue = new JProperty(objItem.Name, strings);
                        }
                    }
                    if (i == 0)
                    {
                        rootClass.Add(kewValue);
                    }
                    else
                    {
                        Objects.Add(kewValue);
                    }
                }
                Object.Add(Objects);
            }
            for (int i = 1; i < Object.Count(); i++)
            {
                string Objectname = "Object" + objects[i].ObjectID.ToString();
                JProperty kewValue = new JProperty(Objectname, Object[i]);
                rootClass.Add(kewValue);
            }

            return rootClass;
        }
    }
}
